package com.example.demo.controller.jfjcbs;

import com.alibaba.fastjson.JSON;
import com.example.demo.log.JsonLog;
import com.example.demo.util.HttpClientUtils;
import com.example.demo.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/*
 * 图像处理
 */
@RestController
@RequestMapping("/image/handler")
@Slf4j
public class ImageHandlerController {

    private static final Logger logger = LoggerFactory.getLogger(ImageHandlerController.class);

//    @Value("${custom.trs.speech}")
    private String speechHost = "http://10.101.221.237/DL-IVRS";
    private static final String SPEECH_MAT_URL = "/matting/image/url";
    private static final String SPEECH_WATER_URL = "/watermark/image/url";



    @PostMapping("extract")
    public String extract(HttpServletRequest request, @RequestBody SpeechImageReq req) {
        log.info("ImageHandlerController--->extract--->req:{}", JsonLog.toJson(req));
        String url = speechHost + SPEECH_MAT_URL;
        HashMap<String, String> paramFormData = new HashMap<>();
//        paramFormData.put("url",req.getUrl().replace("172.29.0.51","10.101.221.232").replace("172.29.0.44", "10.101.221.222"));
        paramFormData.put("url",req.getImageUrl().replace("http://10.101.221.232","https://rhcb.junhaocloud.cn").replace("http://10.101.221.222:3600", "https://rhcb.junhaocloud.cn").replace("http://172.29.0.51", "https://rhcb.junhaocloud.cn").replace("http://172.29.0.44:3600", "https://rhcb.junhaocloud.cn"));
        HashMap<String, Object> heardMap = new HashMap<>();
        heardMap.put("accept","application/json");
        heardMap.put("Content-Type","application/x-www-form-urlencoded");
        SpeechImageMattingResp speechImageMattingResp;
        String response = null;
        try {
            log.info("ImageHandlerController--->图片提取--->输入：url:{},paramFormData:{}", url,JsonUtil.toJson(paramFormData));
            response = HttpClientUtils.doPost(url, paramFormData, heardMap, false);
            log.info("ImageHandlerController--->图片提取--->输出：response:{}", response);
            speechImageMattingResp = JSON.parseObject(response, SpeechImageMattingResp.class);
            if (speechImageMattingResp.getCode() == 1 && !StringUtils.isEmpty(speechImageMattingResp.getResult())) {
                speechImageMattingResp.setResult(speechImageMattingResp.getResult().replace("172.17.0.4","10.101.221.237"));
                return speechImageMattingResp.getResult();
            } else {
                return "失败";
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e,response);
//            throw new CheckedException(Result.RPC_IO_ERROR, "返回值为："+response);
        }
        return "成功";
    }

    @PostMapping("watermark")
    public String watermark(HttpServletRequest request, @RequestBody SpeechImageReq req) {
        log.info("ImageHandlerController--->watermark--->req:{}", JsonLog.toJson(req));
        String url = speechHost + SPEECH_WATER_URL;
        HashMap<String, String> paramFormData = new HashMap<>();
//        paramFormData.put("url",req.getUrl().replace("172.29.0.51","10.101.221.232").replace("172.29.0.44", "10.101.221.222"));
        paramFormData.put("url",req.getImageUrl().replace("http://10.101.221.232","https://rhcb.junhaocloud.cn").replace("http://10.101.221.222:3600", "https://rhcb.junhaocloud.cn").replace("http://172.29.0.51", "https://rhcb.junhaocloud.cn").replace("http://172.29.0.44:3600", "https://rhcb.junhaocloud.cn"));
        HashMap<String, Object> heardMap = new HashMap<>();
        heardMap.put("accept","application/json");
        heardMap.put("Content-Type","application/x-www-form-urlencoded");
        SpeechImageWaterResp speechImageWaterResp;
        String response = null;
        try {
            log.info("ImageHandlerController--->去水印--->输入：url:{},paramFormData:{}", url,JsonUtil.toJson(paramFormData));
            response = HttpClientUtils.doPost(url, paramFormData, heardMap, false);
            log.info("ImageHandlerController--->去水印--->输出：response:{}", response);
            speechImageWaterResp = JSON.parseObject(response, SpeechImageWaterResp.class);

        } catch (Exception e) {
            log.error(e.getMessage(), e,response);
//            throw new CheckedException(Result.RPC_IO_ERROR, "返回值为："+response);
        }
        return "成功";
    }


}